package jp.eisbahn.eclipse.plugins.twitterclipse.internal;

import java.text.DateFormat;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import jp.eisbahn.eclipse.plugins.twitterclipse.TwitterStatus;
import jp.eisbahn.eclipse.plugins.twitterclipse.TwitterUser;

import org.apache.commons.lang.builder.EqualsBuilder;

public class TwitterStatusImpl implements TwitterStatus {
	
	private String created_at;
	
	private String id;
	
	private String text;
	
	private TwitterUserImpl user;

	public String getCreatedAt() {
		return created_at;
	}
	
	public Date getCreatedAtDate() {
		DateFormat format1 = new SimpleDateFormat("EEE MMM d HH:mm:ss Z yyyy", Locale.US);
		DateFormat format2 = new SimpleDateFormat("EEE d MMM HH:mm:ss Z yyyy", Locale.US);
		Date thisDate = format1.parse(created_at, new ParsePosition(0));
		if (thisDate == null) {
			thisDate = format2.parse(created_at, new ParsePosition(0));
		}
		return thisDate;
	}

	public void setCreated_at(String created_at) {
		this.created_at = created_at;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}
	
	public TwitterUser getTwitterUser() {
		return user;
	}
	
	public void setUser(TwitterUserImpl user) {
		this.user = user;
	}

	public int compareTo(TwitterStatus o) {
		DateFormat format1 = new SimpleDateFormat("EEE MMM d HH:mm:ss Z yyyy", Locale.US);
		DateFormat format2 = new SimpleDateFormat("EEE d MMM HH:mm:ss Z yyyy", Locale.US);
		Date thisDate = format1.parse(created_at, new ParsePosition(0));
		if (thisDate == null) {
			thisDate = format2.parse(created_at, new ParsePosition(0));
		}
		Date targetDate = format1.parse(o.getCreatedAt(), new ParsePosition(0));
		if (targetDate == null) {
			targetDate = format2.parse(o.getCreatedAt(), new ParsePosition(0));
		}
		return thisDate.compareTo(targetDate);
	}

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof TwitterStatusImpl) {
			TwitterStatusImpl target = (TwitterStatusImpl)obj;
			return new EqualsBuilder().append(user.getId(), target.user.getId()).isEquals();
		} else {
			return false;
		}
	}

	@Override
	public int hashCode() {
		return user.getId().hashCode();
	}

}
